home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Sample Code / DTS QT Utilities.May-95 / Projects & Test Apps / QT Internals / Mac Framework / MacFramework.c < prev    next >
Encoding:
Text File  |  1995-04-30  |  19.4 KB  |  794 lines  |  [TEXT/MPCC]

  1. /*
  2.     File:        MacFramework.c
  3.  
  4.     Contains:    Basic Macintosh Functions for Window, Menu handling and similar things.
  5.  
  6.     Written by:    DTS
  7.  
  8.     Copyright:    © 1994-1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.        <1>         12/20/94    khs        first file
  13.        
  14. */
  15.  
  16.  
  17. // INCLUDES
  18. #include <SegLoad.h>
  19. #include <ToolUtils.h>
  20. #include <Devices.h>
  21. #include <Fonts.h>
  22.  
  23. #include "DTSQTUtilities.h"
  24. #include "AppConfiguration.h"
  25. #include "MacFramework.h"
  26.  
  27.  
  28. // WINDOW DEFINITIONS
  29. const Rect kDefaultWinRect;
  30. Rect kLimitRect = {0, 0, 480, 640};                            //max size for any window
  31. long gMCFlags = kMCFlags;
  32.  
  33.  
  34. // GLOBALS
  35. Boolean gQuitFlag = false;                                        // Flag that keeps track of termination state.
  36. unsigned long gWNEsleep = kWNEDefaultSleep;        // WaitNextEvent sleep time.
  37. Str255 gWindowTitle = "\pUntitled";                    // Default name for created windows.
  38.  
  39.  
  40. // PURE MAC TOOLBOX FUNCTIONS
  41.  
  42. // ______________________________________________________________________
  43. void InitMacEnvironment(long nMasters)
  44. {
  45.     long i;
  46.     MaxApplZone();
  47.     
  48.     for(i = 0; i <nMasters; i++)
  49.         MoreMasters();
  50.     
  51.     InitGraf(&qd.thePort);
  52.     InitFonts();
  53.     InitWindows();
  54.     InitMenus();
  55.     FlushEvents(everyEvent, 0);
  56.     TEInit();
  57.     InitCursor();
  58.     InitDialogs(NULL);
  59. }
  60.  
  61.  
  62. // ______________________________________________________________________
  63. void InitStack(long extraStackSpace)
  64. {
  65.     Ptr size = GetApplLimit();
  66.     SetApplLimit(size - extraStackSpace);    // make room on the stack
  67. }
  68.  
  69.  
  70. // ______________________________________________________________________
  71. Boolean InitMenubar(void)
  72. {
  73.     Handle aMenuHandle = NULL;
  74.     
  75.     aMenuHandle = GetNewMBar(mMenubar); DebugAssert(aMenuHandle != NULL);
  76.     if(aMenuHandle == NULL)
  77.         return false;
  78.     SetMenuBar(aMenuHandle);
  79.     DisposeHandle(aMenuHandle);  DebugAssert(MemError() == noErr);
  80.     
  81.     AddResMenu(GetMHandle(mApple), 'DRVR');
  82.  
  83.     DrawMenuBar();
  84.     return true;
  85. }
  86.  
  87.  
  88. // ______________________________________________________________________
  89. void HandleMenuCommand(long theMenuResult)
  90. {
  91.     short             aMenuID, aMenuItem;
  92.     Str255            daName;
  93.     
  94.     aMenuID = HiWord(theMenuResult);
  95.     aMenuItem = LoWord(theMenuResult);
  96.     
  97.     switch(aMenuID)
  98.     {
  99.         // APPLE MENU
  100.         case mApple:
  101.             switch(aMenuItem)
  102.             {
  103.                 case iAbout:    // about box
  104.                     ShowAboutDialogBox();     
  105.                     break;
  106.                 
  107.                 default:     // Apple menu handling
  108.                     GetItem(GetMHandle(mApple), aMenuItem, daName);
  109.                     (void)OpenDeskAcc(daName);
  110.                     break;
  111.             } // end switch(aMenuItem)
  112.             break;
  113.  
  114.         // FILE MENU            
  115.         case mFile:
  116.             switch(aMenuItem)
  117.             {
  118.                 case iNew:
  119.                     {
  120.                         if ( !DoCreateNewMovie())
  121.                         {
  122.                             SysBeep(kDefaultSysBeep); break;
  123.                         }
  124.                     }
  125.                     break;
  126.                     
  127.                 case iOpen:
  128.                     if (!DoCreateMovieWindow( NULL) )
  129.                     {
  130.                         SysBeep(kDefaultSysBeep); break;
  131.                     }
  132.                     break;
  133.                 
  134.                 case iClose:
  135.                     DoDestroyMovieWindow( FrontWindow() );
  136.                     break;
  137.                 
  138.                 case iSave:
  139.                     {
  140.                         if( !DoUpdateMovieFile( FrontWindow()) )
  141.                         {
  142.                             SysBeep(kDefaultSysBeep); break;
  143.                         }
  144.                     }
  145.                     break;
  146.                     
  147.                 case iSaveAs:
  148.                     {
  149.                     MovieController mc;
  150.                     
  151.                     mc = GetMCFromFrontWindow();
  152.                     if(mc == NULL) 
  153.                     {
  154.                         SysBeep(kDefaultSysBeep); break;
  155.                     }
  156.                      if( QTUSaveMovie(MCGetMovie(mc)) != noErr)
  157.                          SysBeep(kDefaultSysBeep);
  158.                      }
  159.                     break;
  160.                                     
  161.                 case iPrint:
  162.                     {
  163.                         MovieController mc;
  164.                         Boolean aResult;
  165.  
  166.                         mc = GetMCFromFrontWindow();
  167.                         if(mc != NULL)
  168.                         {
  169.                             aResult = QTUPrintMoviePoster( MCGetMovie(mc), kDefaultX, kDefaultY); 
  170.                             if(aResult == false)
  171.                                 SysBeep(kDefaultSysBeep);
  172.                         }
  173.                         else
  174.                                 SysBeep(kDefaultSysBeep);
  175.                         break;
  176.                     }
  177.  
  178.                 case iQuit:
  179.                     {
  180.                         gQuitFlag = true;
  181.                         break;
  182.                     }
  183.  
  184.             } // end switch(aMenuItem), mFile
  185.             break;
  186.     
  187.         
  188.         // EDIT MENU
  189.         // Provide the default controller cut, copy and paste functionality.    
  190.         case mEdit:
  191.         {
  192.             Movie aMovie = NULL;
  193.             MovieController mc;
  194.             
  195.             mc = GetMCFromFrontWindow();
  196.             if (mc == NULL) break;
  197.             
  198.             switch(aMenuItem)
  199.             {
  200.                 case iUndo: MCUndo(mc); break;
  201.                 
  202.                 case iCut: aMovie = MCCut(mc); break;
  203.                 
  204.                 case iCopy: aMovie = MCCopy(mc); break;
  205.                 
  206.                 case iPaste: MCPaste(mc, NULL); break;
  207.                 
  208.                 case iClear: MCClear(mc); break;
  209.                 
  210.                 case iSelectAll:  
  211.                     if(QTUSelectAllMovie(mc) != noErr)
  212.                         SysBeep(kDefaultSysBeep);
  213.                     break;
  214.             } // end switch(aMenuItem)
  215.             
  216.             if(aMovie)
  217.             {
  218.                 PutMovieOnScrap(aMovie, 0);
  219.                 DisposeMovie(aMovie);  DebugAssert(MemError() == noErr);
  220.             }
  221.             break;
  222.         } // end case mEdit
  223.  
  224.  
  225.     default:
  226.         HandleApplicationMenu(aMenuID, aMenuItem);
  227.         break;
  228.     } // end switch(aMenuID)
  229.     
  230.     HiliteMenu(0);
  231. }
  232.  
  233.  
  234. // ______________________________________________________________________
  235. void AdjustMenus(void)
  236. {
  237.     WindowRef             aWindow;
  238.     MovieController    mc;
  239.     WindowObject        aWindowObject;
  240.     
  241.     aWindow = FrontWindow();
  242.  
  243.     if(aWindow != NULL)
  244.     {
  245.         // Enable the close entry of we have windows = movies.
  246.         EnableItem( GetMHandle(mFile), iClose);
  247.         
  248.         // Handle the edit menu.
  249.         if( (aWindowObject = (WindowObject)GetWRefCon(aWindow) ) != NULL)
  250.         {
  251.             mc = (**aWindowObject).controller;
  252.             if( (IsWindowObjectOurs(aWindowObject)) && (mc != NULL))
  253.             {
  254.                 MCSetUpEditMenu(mc, 0L, GetMHandle(mEdit));
  255.                 EnableItem(GetMHandle(mEdit), iSelectAll);
  256.         
  257.                 EnableItem(GetMHandle(mFile), iSave);
  258.                 EnableItem(GetMHandle(mFile), iSaveAs);
  259.                 EnableItem(GetMHandle(mFile), iClose);
  260.                 EnableItem(GetMHandle(mFile), iPrint);
  261.             }
  262.         }
  263.     } // end if(aWindow != NULL)
  264.     else 
  265.     {
  266.         DisableItem(GetMHandle(mFile), iSave);
  267.         DisableItem(GetMHandle(mFile), iSaveAs);
  268.         DisableItem(GetMHandle(mFile), iClose);
  269.         DisableItem(GetMHandle(mFile), iPrint);
  270.         
  271.         DisableItem(GetMHandle(mEdit), iCut);
  272.         DisableItem(GetMHandle(mEdit), iCopy);
  273.         DisableItem(GetMHandle(mEdit), iPaste);
  274.         DisableItem(GetMHandle(mEdit), iUndo);
  275.         DisableItem(GetMHandle(mEdit), iClear);
  276.         DisableItem(GetMHandle(mEdit), iSelectAll);
  277.         
  278.     }
  279.     
  280.     AdjustApplicationMenus();                    // fix any specific app menus as well.
  281. }
  282.  
  283.  
  284. // ______________________________________________________________________
  285. void MainEventLoop(void)
  286. {
  287.     EventRecord             anEvent;
  288.     WindowRef            whichWindow, aWindow;
  289.     Boolean                    aMovieEvent;
  290.     short                    aWindowPart;
  291.     Rect                        aScreenRect;
  292.     Rect                        aRefreshArea;
  293.     Point                        aPoint  = {100, 100};
  294.     WindowObject        aWindowObject;
  295.     MovieController    mc;
  296.     Boolean                    siouxHandled;
  297.     
  298.     while(!gQuitFlag)
  299.     {
  300.         WaitNextEvent(everyEvent, &anEvent, gWNEsleep, NULL);
  301.         
  302. #ifdef USESIOUX
  303.         siouxHandled = SIOUXHandleOneEvent(&anEvent);
  304. #endif USESIOUX
  305.  
  306.         AdjustMenus();
  307.         aMovieEvent = false;
  308.         
  309.         if( (whichWindow = FrontWindow() ) != NULL)
  310.             DoIdle(whichWindow);
  311.  
  312.         // First, let the movie controller have access to the event.
  313.         for( aWindow = FrontWindow(); aWindow != NULL ; aWindow = (WindowPtr)((WindowPeek)aWindow)->nextWindow)
  314.             if(( aWindowObject = (WindowObject)GetWRefCon(aWindow)) != NULL)         
  315.                 if((IsWindowObjectOurs(aWindowObject)) && ( (mc = (**aWindowObject).controller) != NULL) ) 
  316.                     if(MCIsPlayerEvent(mc, &anEvent)) 
  317.                         aMovieEvent = true ;
  318.                     
  319.     // Then, if this wasn't a movie controller event, pass it on to the case statement that dispatches the event
  320.     // to the right function.
  321.     if(!aMovieEvent)
  322.     {
  323.         switch(anEvent.what)
  324.         {
  325.             case mouseDown:
  326.                 aWindowPart = FindWindow(anEvent.where, &whichWindow);
  327.  
  328.                 // Window related events:            
  329.                 switch(aWindowPart)
  330.                 {
  331.                     case inMenuBar:
  332.                         HandleMenuCommand(MenuSelect(anEvent.where));
  333.                         break;
  334.                         
  335.                     case inDrag:
  336.                     {
  337.                         Rect                         aRect;
  338.                         Movie                    aMovie = NULL;
  339.                         MovieController     mc = NULL;
  340.                         WindowObject         aWindowObject = NULL;
  341.                         
  342.                         aWindowObject = (WindowObject)GetWRefCon(whichWindow);
  343.                         mc = (**aWindowObject).controller;
  344.                         if (! (IsWindowObjectOurs(aWindowObject)) && (mc == NULL))
  345.                             break;
  346.                             
  347.                         aMovie = MCGetMovie(mc);
  348.                         
  349.                         GetMovieBox(aMovie, &aRect);
  350.                         aScreenRect = (**GetGrayRgn()).rgnBBox;
  351.                         DragAlignedWindow(whichWindow, anEvent.where, &aScreenRect, &aRect, NULL);
  352.                     }  // end case inDrag;    
  353.                         break;
  354.                         
  355.                     case inContent:
  356.                         SelectWindow(whichWindow);
  357.                         HandleContentClick(whichWindow, &anEvent);
  358.                         break;
  359.                     
  360.                     case inGoAway:
  361.                         // if the window is closed, dispose the movie, the controller and the window
  362.                         if( TrackGoAway(whichWindow, anEvent.where) )
  363.                             DoDestroyMovieWindow(whichWindow);
  364.                         break;
  365.                 } // end switch(aWindowPart):
  366.                 break;
  367.  
  368.                 // System level events:
  369.                 case updateEvt:
  370.                     whichWindow = (WindowRef)anEvent.message;
  371.                     aRefreshArea = ((**(whichWindow->visRgn)).rgnBBox);
  372.                     DoUpdateWindow(whichWindow, &aRefreshArea);
  373.                     break;
  374.                     
  375.                 case keyDown:
  376.                 case autoKey:
  377.                     HandleKeyPress(&anEvent);
  378.                     break;
  379.                 
  380.                 case diskEvt:
  381.                     if(HiWord(anEvent.message) != noErr)
  382.                         (void)DIBadMount(aPoint, anEvent.message);
  383.                     break;
  384.                 
  385.                 case activateEvt:
  386.                     whichWindow = (WindowRef)anEvent.message;
  387.                     
  388.                      if ( IsAppWindow(whichWindow) )
  389.                     {
  390.                         DoActivateWindow(whichWindow, ((anEvent.modifiers & activeFlag) != 0 ));
  391.                     }
  392.                     break;
  393.                     
  394.                 case osEvt:
  395.                     switch(( anEvent.message > 24) & 0x00FF )        // get high byte of word
  396.                     {
  397.                         case suspendResumeMessage:
  398.                             if( FrontWindow() )
  399.                             {
  400.                                 DoActivateWindow(FrontWindow(), !((anEvent.message & resumeFlag) == 0));
  401.                             }
  402.                             break;
  403.                         
  404.                         case mouseMovedMessage:
  405.                             break;
  406.                     } // end switch(anEvent.message > 24) & 0x00FF)    
  407.                     break;
  408.                 
  409.                 case nullEvent:
  410.                     if(( whichWindow = FrontWindow() ) != NULL)
  411.                         DoIdle(whichWindow);
  412.                     break;
  413.         } // end switch(anEvent.what)
  414.     } // end if(!aMovieEvent)    
  415.     } // end while(!gQuitFlag)
  416. }
  417.  
  418.  
  419. // ______________________________________________________________________
  420. Boolean IsAppWindow(WindowRef theWindow)
  421. {
  422.     short aWindowKind;
  423.     
  424.     if (theWindow == NULL)
  425.         return false;
  426.     else
  427.     {
  428.         aWindowKind = ((WindowPeek)theWindow)->windowKind;
  429.         return ( (aWindowKind >= userKind) || (aWindowKind == dialogKind) );
  430.     }
  431. }
  432.  
  433.  
  434. // ______________________________________________________________________
  435. WindowObject CreateWindowObject(WindowRef theWindow)
  436. {
  437.     WindowObject aWindowObject = NULL;
  438.     
  439.     // WindowObjectRecord = 90 bytes (good to know if chasing for handles in the heap).
  440.     aWindowObject = (WindowObject)NewHandle(sizeof(WindowObjectRecord));
  441.     
  442.     if(aWindowObject != NULL)
  443.     {
  444.         (**aWindowObject).controller = NULL;
  445.         (**aWindowObject).ObjectType = kMovieControllerObject;
  446.         
  447.         SetWRefCon(theWindow, (long)aWindowObject);        // store a ref to the record/handle into the window
  448.     }
  449.     
  450.     return aWindowObject;
  451. }
  452.  
  453.  
  454. // ______________________________________________________________________
  455. void HandleKeyPress(EventRecord *theEvent)
  456. {
  457.     char aKey;
  458.     
  459.     aKey = theEvent->message & charCodeMask;
  460.     
  461.     if(theEvent->modifiers & cmdKey)        // command key down?
  462.     {
  463.         HandleMenuCommand(MenuKey(aKey));
  464.     }
  465. }
  466.  
  467.  
  468. // ______________________________________________________________________
  469. void ShowAboutDialogBox(void)
  470. {
  471.     DialogPtr aDialog;
  472.     short         itemHit;
  473.     FontInfo    aFontInfo;
  474.     GrafPtr        aSavedPort;
  475.     
  476.     GetPort(&aSavedPort);
  477.     aDialog = GetNewDialog(kAboutBox, NULL, (WindowPtr) - 1L); DebugAssert(aDialog != NULL);
  478.     SetPort(aDialog);
  479.  
  480.     // Change font to Geneva, 9pt, bold, just for the sake of it...
  481.     TextFont(applFont); TextSize(9); TextFace(bold);
  482.     GetFontInfo(&aFontInfo);
  483.     
  484.     (*((DialogPeek)aDialog)->textH)->txFont = applFont;
  485.     (*((DialogPeek)aDialog)->textH)->txSize = 9;
  486.     (*((DialogPeek)aDialog)->textH)->lineHeight = aFontInfo.ascent + aFontInfo.descent + aFontInfo.leading;
  487.     (*((DialogPeek)aDialog)->textH)->fontAscent = aFontInfo.ascent;
  488.  
  489.     SetDialogDefaultItem(aDialog, 1);
  490.         
  491.     do
  492.     {
  493.         ModalDialog(NULL, &itemHit);
  494.     } while(itemHit != ok);
  495.     
  496.     SetPort(aSavedPort);
  497.     DisposeDialog(aDialog);  DebugAssert(MemError() == noErr);
  498. }
  499.  
  500.  
  501. // MOVIE RELATED TOOLBOX FUNCTIONS
  502.  
  503. // ______________________________________________________________________
  504. MovieController  GetMCFromFrontWindow(void)
  505. {
  506.     MovieController     mc = NULL;
  507.     WindowRef             aWindow = NULL;
  508.     WindowObject        aWindowObject = NULL;
  509.     Movie                    aMovie = NULL;
  510.     OSErr                    anErr = noErr;
  511.     OSType                    aType = NULL;
  512.  
  513.     if( ( aWindow = FrontWindow() ) == NULL )
  514.         return NULL;
  515.  
  516.     if( !IsAppWindow(aWindow) )
  517.         return NULL;
  518.             
  519.     aWindowObject = (WindowObject)GetWRefCon(aWindow);
  520.     if(aWindowObject == NULL)
  521.         return NULL;
  522.         
  523.     MoveHHi((Handle)aWindowObject); HLock((Handle)aWindowObject);
  524.  
  525.     // Test if this is indeed a movie controller, and not an otherwise valid pointer (non-NULL value)
  526.     if(!IsWindowObjectOurs(aWindowObject))
  527.         return NULL;
  528.         
  529.     mc = (**aWindowObject).controller;
  530.     HUnlock((Handle)aWindowObject);
  531.     
  532.     return mc;
  533. }
  534.  
  535.  
  536. // ______________________________________________________________________
  537. Boolean IsWindowObjectOurs(WindowObject theObject)
  538. {
  539.     OSType        aType = NULL;
  540.     
  541.     aType = (**theObject).ObjectType;
  542.     if(aType == kMovieControllerObject)
  543.         return true;
  544.     else return false;
  545. }
  546.  
  547.  
  548.  
  549. // ______________________________________________________________________
  550. Boolean DoCreateNewMovie(void)
  551. {
  552.     Movie aMovie = NULL;
  553.     
  554.     aMovie = NewMovie(newMovieActive); DebugAssert(aMovie != NULL);
  555.     if(aMovie == NULL)
  556.         return false;
  557.     
  558.     if(!DoCreateMovieWindow(aMovie))
  559.         return false;
  560.     else    
  561.         return true;
  562. }
  563.  
  564.  
  565. // ______________________________________________________________________
  566. Boolean DoCreateMovieWindow(Movie theMovie)
  567. {
  568.     Rect                         aRect = kDefaultWinRect;
  569.     WindowRef            aWindow = NULL;
  570.     MovieController    mc = NULL;
  571.     WindowObject        aWindowObject = NULL;
  572.     GrafPtr                    aSavedPort;
  573.     short                    aRefNum;
  574.     short                    aResID;
  575.     FSSpec                    aFileFSSpec;
  576.     
  577.     aFileFSSpec.vRefNum = 0;            // we want to use the FSSpec later
  578.  
  579.     GetPort(&aSavedPort);
  580.     aWindow = CreateMovieWindow(&aRect, gWindowTitle);
  581.     SetPort((GrafPtr) aWindow);
  582.     
  583.     if(aWindow == NULL)
  584.         return false;
  585.     
  586.     aWindowObject = CreateWindowObject(aWindow);
  587.     if(aWindowObject == NULL)
  588.         return false;
  589.  
  590. //If we don't get a movie, call the internal QTUGetMovie that will get us one.
  591.     if(theMovie == NULL)
  592.     {
  593.         theMovie = QTUGetMovie(&aFileFSSpec, &aRefNum, &aResID);  DebugAssert(theMovie != NULL);
  594.  
  595.         // Add the FSSpec, refnum and resid values to the window object (we need these when we save the movie).
  596.         (**aWindowObject).FileFSSpec = aFileFSSpec;
  597.         (**aWindowObject).FileRefNum = aRefNum;
  598.         (**aWindowObject).FileResID = aResID;
  599.  
  600.         // Get movie title and set this to the window title.
  601.         SetWTitle(aWindow, aFileFSSpec.name);
  602.     }
  603.  
  604.     
  605.     if(theMovie != NULL)
  606.     {
  607.         SetMovieGWorld(theMovie, (CGrafPtr)aWindow, 0);        // make sure the movie uses the window GWorld at all situations
  608.         mc = SetupMovieWindowWithController(theMovie, aWindow);
  609.     }
  610.     
  611.     ShowWindow(aWindow);
  612.     SelectWindow(aWindow);                                    // make it front-most as it's just created
  613.     InvalRect( &((GrafPtr)aWindow)->portRect);
  614.     
  615.     MCEnableEditing(mc, true);                                // enable the default movie controller editing
  616.     
  617.     SetPort(aSavedPort);
  618.     return true;
  619. }
  620.  
  621.  
  622. // ______________________________________________________________________
  623. MovieController SetupMovieWindowWithController(Movie theMovie, WindowRef theWindow)
  624. {
  625.     MovieController     mc;
  626.     Rect                        aRect;
  627.     GrafPtr                    aSavedPort;
  628.     WindowObject        aWindowObject;
  629.     short                    aMovieWidth;
  630.     short                    aMovieHeight;
  631.     
  632.     DebugAssert(theMovie != NULL); 
  633.     DebugAssert(theWindow != NULL);
  634.     
  635.     aWindowObject = (WindowObject)GetWRefCon(theWindow);            // Get our window specific data.
  636.     if(!IsWindowObjectOurs(aWindowObject))
  637.         return NULL;                                                                                // Quick sanity test of the window created.
  638.     GetPort(&aSavedPort);
  639.     SetPort( (GrafPtr)theWindow);
  640.     
  641. // Resize the movie bounding rect.
  642.     GetMovieBox(theMovie, &aRect);     SetMovieBox(theMovie, &aRect);
  643.  
  644. // Create the movie controller.    
  645.     mc = NewMovieController(theMovie, &aRect, gMCFlags);
  646.     if(mc == NULL)
  647.         return NULL;
  648.     MCGetControllerBoundsRect(mc, &aRect);
  649.     
  650. // Add grow box for the movie controller and also an action filter that resizes the controllers
  651.     MCDoAction(mc, mcActionSetGrowBoxBounds, &kLimitRect);
  652.     MCSetActionFilterWithRefCon(mc, 
  653.                                                     NewMCActionFilterWithRefConProc(QTUResizeMCActionFilter),
  654.                                                     (long) theWindow);
  655.                                                     
  656.     // Check if the bounding rects are sane.
  657.     aMovieWidth = aRect.right - aRect.left;
  658.     aMovieHeight = aRect.bottom - aRect.top;
  659.     
  660.     aRect.top = aRect.left  = 0;
  661.     aRect.right = aMovieWidth;
  662.     aRect.bottom = aMovieHeight;
  663.     
  664.     // Resize the window as well.
  665.     SizeWindow(theWindow, aMovieWidth, aMovieHeight, true);
  666.     MoveWindow(theWindow, kDefaultX, kDefaultY, false);                                
  667.  
  668.     SetPort(aSavedPort);
  669.  
  670.     // Add any additional controller functionality.
  671.     AddControllerFunctionality(mc);
  672.  
  673.     // Save important stuff into the window object     and the original size of the movie
  674.     {    
  675.     Rect aRect;
  676.     OSErr anErr;
  677.  
  678.     (**aWindowObject).controller = mc;
  679.     anErr = MCGetControllerBoundsRect(mc, &aRect); DebugAssert(anErr == noErr);
  680.     (**aWindowObject).originalSize = aRect;
  681.     }
  682.     
  683.     return mc;
  684. }
  685.  
  686.  
  687. // ______________________________________________________________________
  688. Boolean DoUpdateMovieFile(WindowRef theWindow)
  689. {
  690.     Movie                     aMovie = NULL;
  691.     WindowObject        aWindowObject = NULL;
  692.     MovieController    mc = NULL;
  693.     OSErr                    anErr;
  694.     
  695.     if ( (theWindow == NULL) || !IsAppWindow(theWindow) )
  696.         return false;
  697.         
  698.     aWindowObject = (WindowObject)GetWRefCon(theWindow); DebugAssert(aWindowObject != NULL);
  699.     mc = (**aWindowObject).controller; DebugAssert(mc != NULL);
  700.     
  701.     if( !(IsWindowObjectOurs(aWindowObject)) && (mc == NULL) )
  702.         return false;
  703.     
  704.     aMovie = MCGetMovie(mc); DebugAssert(aMovie != NULL);
  705.     if(aMovie == NULL)
  706.         return false;
  707.         
  708.     if( (**aWindowObject).FileRefNum == -1)                    // brand new movie, no file attached to it.
  709.     {
  710.         if ( QTUSaveMovie(aMovie) != noErr)
  711.             return false;    
  712.     }
  713.     else                                                                            // we have an existing file, just update the movie resource
  714.     {
  715.         // Open the movie resource file, update the resource, and then close it again!
  716.         anErr = OpenMovieFile(& (**aWindowObject).FileFSSpec, & (**aWindowObject).FileRefNum, fsRdWrPerm);
  717.         DebugAssert(anErr == noErr);
  718.         if(anErr != noErr)
  719.             return false;
  720.         
  721.         anErr = UpdateMovieResource(aMovie, (**aWindowObject).FileRefNum, (**aWindowObject).FileResID, NULL);
  722.         DebugAssert(anErr == noErr);
  723.         
  724.         CloseMovieFile( (**aWindowObject).FileRefNum );
  725.     }
  726.     
  727.     if(anErr == noErr)
  728.         return true;
  729.     else
  730.         return false;
  731. }
  732.  
  733.  
  734. // ______________________________________________________________________
  735. void DoDestroyMovieWindow(WindowRef theWindow)
  736. {
  737.     Movie                     aMovie;
  738.     MovieController    mc;
  739.     WindowObject        aWindowObject;
  740.     
  741.     DebugAssert(theWindow != NULL); if(theWindow == NULL) return;
  742.     
  743.     aWindowObject =(WindowObject)GetWRefCon(theWindow);
  744.     MoveHHi((Handle)aWindowObject);
  745.     HLock((Handle)aWindowObject);
  746.     
  747.     if ( IsWindowObjectOurs(aWindowObject)) // our window?
  748.     {
  749.         mc = (**aWindowObject).controller;
  750.         aMovie = MCGetMovie(mc);
  751.         
  752.         if(aMovie != NULL)
  753.             DisposeMovie(aMovie); DebugAssert(MemError() == noErr);
  754.     
  755.         if(mc != NULL)
  756.             DisposeMovieController(mc); DebugAssert(MemError() == noErr);
  757.     
  758.         if( (**aWindowObject).FileRefNum != -1)
  759.             CloseMovieFile((**aWindowObject).FileRefNum);
  760.         
  761.         (**aWindowObject).ObjectType = NULL;
  762.         (**aWindowObject).controller = NULL;
  763.         (**aWindowObject).FileResID = NULL;
  764.         (**aWindowObject).FileRefNum = NULL;
  765.         
  766.         DisposeHandle((Handle)aWindowObject); DebugAssert(MemError() == noErr);
  767.         DisposeWindow(theWindow); DebugAssert(MemError() == noErr);
  768.         
  769.         CompactMem(0xFFFFFFFF);        //We might as well compact the mem here for getting better performance later.
  770.     }
  771. }
  772.  
  773.  
  774. // ______________________________________________________________________
  775. void DoActivateWindow(WindowRef theWindow, Boolean becomingActive)
  776. {
  777.     WindowObject         aWindowObject = NULL;
  778.     MovieController    mc = NULL;
  779.     GrafPtr                    aSavedPort = NULL;
  780.     
  781.     GetPort(&aSavedPort);
  782.     SetPort((GrafPtr)theWindow);
  783.     
  784.     if( (aWindowObject = (WindowObject)GetWRefCon(theWindow)) != NULL)
  785.     {
  786.         mc = (**aWindowObject).controller;
  787.         if( (IsWindowObjectOurs(aWindowObject)) && (mc != NULL) )
  788.             MCActivate(mc, theWindow, becomingActive);
  789.     }
  790.     SetPort(aSavedPort);
  791. }
  792.  
  793.  
  794.